# -*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name:        log
# Purpose:
#
# Author:      wei
#
# Created:     05/07/2012
# Copyright:   (c) wei 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import logging
import logging.handlers
import sys,os

LOGGING_MSG_FORMAT  = '%(name)s> %(levelname)s [%(asctime)s]: %(message)s\r\n'
LOGGING_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'

def get_logger(logname,modlename):
    root_logger = logging.getLogger(logname)
    if not root_logger.hasHandlers():
        path=os.path.join(sys.path[0],'logs').replace('\\','/')
        if not os.path.isdir(path):
            os.mkdir(path)
        logFile='%s/%s.log' % (path,logname)
        #handler=logging.handlers.RotatingFileHandler(logFile,mode='a',
        #    maxBytes=1024*1024*10,backupCount=10)#encoding='utf-8'
        handler=logging.handlers.TimedRotatingFileHandler(logFile,'midnight',1)
        #handler.suffix = "%Y-%m-%d"
        #handler.setLevel(logging.INFO)
        fmter=logging.Formatter(LOGGING_MSG_FORMAT,LOGGING_DATE_FORMAT)
        handler.setFormatter(fmter)
        root_logger.addHandler(handler)
        root_logger.setLevel(logging.DEBUG)

    s=logname+'.'+modlename
    return logging.getLogger(s)

def __test():
    logger = get_logger('aaa',__name__)
    logger.warning('中文abc')
    logger.info("info")
    logger.error('error:%s','XXXX')

if __name__ == '__main__':
    __test()
